<?php

/* * ******* DB Access functions ******** */
include 'conn.php';
include_once 'config.php';

function OpenDB() {
    $link = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
    @mysql_select_db(DB_DATABASE);
    return $link;
}

function CloseDB(&$link) {
    mysql_close($link);
}

function GetSQL($query) {
    $result = mysql_query($query);
    if ($result) {
        while ($row = @mysql_fetch_assoc($result))
        {
            $ret[] = $row;
        }
        return (isset($ret)) ? $ret : false;
    }
    else
        return false;
}


function GetSQLFirstValue($query) {
    $result = mysql_query($query);
    if ($result && mysql_num_rows($result) > 0)
        return mysql_result($result, 0, 0);
    else
        return false;
}

function GetValue($key) {
    $query = "select _value from key_value where  _key = '" . $key . "'";
    $result = mysql_query($query);
    if ($result)
        return mysql_result($result, 0, 0);
    else
        return false;
}

function PostSQL($query) {
    $result = mysql_query($query);
    echo mysql_error();
    return $result;
}

function GetAllTableData($table) {
    return GetSql("select * from $table");
}

function GetTableDataByWhere($table, $where) {
    return GetSql("select * from $table where $where");
}

function GetValueByTableWhere($table, $where, $value) {
    OpenDB();
    $query = "select " . $value . " from $table where $where";
    $result = mysql_query($query);
    if ($result)
        return mysql_result($result, 0, 0);
    else
        return false;
}

?>